﻿using DevExpress.XtraEditors.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZY.EntityFrameWork.Caller;
using ZY.EntityFrameWork.Caller.Facade;
using ZY.EntityFrameWork.Core.Model.Attibutes;
using ZY.EntityFrameWork.Core.Model.Dto.Authority;

namespace EF_CodeFist
{
    public partial class FormPermission : Form
    {
        public FormPermission()
        {
            InitializeComponent();
        }

        public string RoleName { get; set; }

        private List<RoleModuleDto> roleModules;//角色的权限表
        private List<ModuleDto> modules;//模块表
        private List<ModuleDto> roleAllModules;//角色的权限表（包含未授权的模块，权限为0）

        #region 权限下拉框

        /// <summary>
        /// 生成下拉列表中的所有权限内容
        /// </summary>
        public void InitPermissions(RepositoryItemCheckedComboBoxEdit ri)
        {
            ri.SetFlags(typeof(PermissionValue));
            //ri.Items[0].Enabled = false;
            removeCombinedFlags(ri);
        }

        /// <summary>
        /// 移除复合项
        /// </summary>
        /// <param name="ri"></param>
        private void removeCombinedFlags(RepositoryItemCheckedComboBoxEdit ri)
        {
            for (int i = ri.Items.Count - 1; i > 0; i--)
            {
                Enum val1 = ri.Items[i].Value as Enum;
                for (int j = i - 1; j >= 0; j--)
                {
                    Enum val2 = ri.Items[j].Value as Enum;
                    if (val1.HasFlag(val2))
                    {
                        ri.Items.RemoveAt(i);
                        break;
                    }
                }
            }
        }

        private List<int> GetDisabledFlag(int permissions)
        {
            List<int> flags = new List<int>();
            int all = Enum.GetValues(typeof(PermissionValue)).Length;
            for (int i = 0; i < all; i++)
            {
                if ((permissions & 1) == 0)//末位为0
                {
                    flags.Add(i);
                }
            }
            return flags;
        }
        #endregion

        /// <summary>
        /// 将模块表中的权限值更新为角色所拥有的权限
        /// </summary>
        private void FillRolePermissions()
        {
            roleAllModules = new List<ModuleDto>();
            roleAllModules.AddRange(modules);//复制模块表

            roleAllModules.ForEach(q =>
                {
                    RoleModuleDto dto = roleModules.Find(h => h.ModuleName == q.ModuleName);
                    if (dto != null)
                    {
                        q.Permissions = dto.Permissions;
                    }
                    else
                        q.Permissions = 0;
                });
        }

        private void FormPermission_Load(object sender, EventArgs e)
        {
            roleModules = CallerFactory.Instance.GetService<IAuthorityService>()
                .GetRolePermissions(RoleName);//角色所有的模块权限

            modules = CallerFactory.Instance.GetService<IAuthorityService>().GetAllModules();
            FillRolePermissions();
            gridControl1.DataSource = modules;

            InitPermissions(checkedComboBoxEdit1);//生成下拉框
            gridView1.Columns[1].ColumnEdit = checkedComboBoxEdit1;
        }

        private void checkedComboBoxEdit1_BeforePopup(object sender, EventArgs e)
        {
            //if(modules[gridView1.FocusedRowHandle].Permissions)
        }

        private void checkedComboBoxEdit1_GetItemEnabled(object sender, DevExpress.XtraEditors.Controls.GetCheckedComboBoxItemEnabledEventArgs e)
        {
            List<int> disabled=GetDisabledFlag(modules[gridView1.FocusedRowHandle].Permissions);
            if (disabled.Contains(e.Index))
            {
                e.Enabled = false;
            }

        }
    }
}
